﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BarcodeDelay.view
{
    public partial class FrmDepSetting : Form
    {
        private DataTable dtProvince = new DataTable();
        public FrmDepSetting()
        {
            InitializeComponent();
            initDataTable();
        }

        private void initDataTable()
        {
            try
            {
                string strFileName = StoParams.Instance.provinceTxtPath;
                if (!File.Exists(strFileName))
                {
                    MessageBox.Show("ConfigOrg.txt 文件不存在，请检查");
                  
                }
                ArrayList arListProvices = UtilityHelp.Instance.getProvinceDelay(strFileName);
                initDtColumn(dtProvince);
                foreach (string strLine in arListProvices)
                {
                    string strNewLine = strLine.Replace("     ", " ");
                    strNewLine = strNewLine.Replace("    ", " ");
                    strNewLine = strNewLine.Replace("   ", " ");
                    strNewLine = strNewLine.Replace("  ", " ");
                    string[] arr = strNewLine.Trim().Split(' ');
                    if(arr.Length>=3)
                    {
                        DataRow drNew = dtProvince.NewRow();
                        drNew[0] = arr[0];
                        drNew[1] = arr[1];
                        drNew[2] = arr[2];
                        dtProvince.Rows.Add(drNew);
                    }
                }

                
            }
            catch(Exception ex)
            {

            }
        }

        private void refreshDgv()
        {
            dgvProvinceSetting.BeginInvoke(new Action(() =>
            {
                BindingSource mbs = new BindingSource();
                mbs.DataSource = dtProvince;
                dgvProvinceSetting.DataSource = mbs;
                for (int i = 0; i < dtProvince.Columns.Count; i++)
                {
                    if (i == dtProvince.Columns.Count - 1)
                    {
                        this.dgvProvinceSetting.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                        this.dgvProvinceSetting.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                    }

                }
            }));
        }

        private void initDtColumn(DataTable dtProvince)
        {
            dtProvince.Columns.Clear();
            dtProvince.Columns.Add(new DataColumn("省份", typeof(string)));
            dtProvince.Columns.Add(new DataColumn("时效下限(整数)", typeof(int)));
            dtProvince.Columns.Add(new DataColumn("时效上限(整数)", typeof(int)));

        }

        private void FrmDepSetting_Load(object sender, EventArgs e)
        {
            refreshDgv();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dtProvince.Rows.Count>0)
            {
                writeTbFile();
                UtilityHelp.Instance.initProvinceDelay();
                MessageBox.Show("设置已保存");
            }
        }
        private void writeTbFile()
        {
            try
            {
                string delFile = System.Environment.CurrentDirectory + "\\" + StoParams.Instance.provinceTxtPath;
                File.Delete(delFile);
                FileStream bFile = new FileStream(delFile, FileMode.Create);
                StreamWriter swb = null;
                if (swb == null)
                {
                    swb = new StreamWriter(bFile);
                }
                foreach(DataRow dr in dtProvince.Rows)
                {
                    string province = dr[0].ToString();
                    string daysStart = dr[1].ToString();
                    string daysEnd = dr[2].ToString();
                    swb.WriteLine(province + " " + daysStart+" "+ daysEnd);
                }
                
                swb.Close();

            }
            catch (Exception ex)
            {

            }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("退出之前请先确认已保存更改？", "退出确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }
            this.Close();
        }
    }
}
